package com.exercises;

class Exercise213 {

    public static void main(String[] args) {
//        rob(new int[]{1, 1, 3, 6, 7, 10, 7, 1, 8, 5, 9, 1, 4, 4, 3});
        System.out.println(rob(new int[]{1, 1, 3, 6, 7, 10, 7, 1, 8}));
    }


    public static int rob(int[] nums) {
        if (nums.length == 1) {
            return nums[0];
        } else if (nums.length == 2) {
            return Math.max(nums[0], nums[1]);
        } else {

        }
        int length = nums.length;
        return Math.max(rob(nums, 0, length - 2), rob(nums, 1, length - 1));
    }

    public static int rob(int[] nums, int start, int end) {
        int[] dp = new int[2];
        dp[0] = nums[start];
        dp[1] = Math.max(nums[start], nums[start + 1]);
        for (int i = start + 2; i <= end; i++) {
            int tmp = dp[1];
            dp[1] = Math.max(dp[0] + nums[i], dp[1]);
            dp[0] = tmp;
        }
        return dp[1];
    }
}
